#include "Thread.h"

thread_t NewThread(proc_t func, void* data)
{
	thread_t ret;
#ifdef WIN32
	ret = CreateThread(NULL, 0, func, data, 0, NULL);
	if (!ret)
	{
		fprintf(stderr, "NewThread: Could not create new thread: %d\n", GetLastError());
		return NULL;
	}
#else
	ret = new pthread_t;
	int res = pthread_create(ret, NULL, func, data);
	if (!res)
	{
		fprintf(stderr, "NewThrad: Could not create new thread: %d\n", res);
		return NULL;
	}
#endif
	return ret;
}

void KillThread(thread_t thread)
{
#ifdef WIN32
	WaitForSingleObject(thread, INFINITE);
#else
	pthread_join(thread);
#endif
}

void PauseThread(int len)
{
#ifdef WIN32
	Sleep(len);
#else
	timespec time;
	time.tv_sec = len / 1000;
	time.tvnsec = (len % 1000) * 1000000;
	nanosleep(&time, NULL);
#endif
}